<@import src="funcs/definePk.ftl"/>
<@import src="funcs/getType.ftl"/>
<#-- 获取查询方式 -->
<#function getQueryMethod methodType>
  <#if methodType == 'like'>
    <#return 'like'/>
  </#if>
  <#if methodType == 'startWith'>
    <#return 'likeLeft'/>
  </#if>
  <#if methodType == 'endWith'>
    <#return 'likeRight'/>
  </#if>
  <#if methodType == 'gt'>
    <#return 'gt'/>
  </#if>
  <#if methodType == 'ge'>
    <#return 'ge'/>
  </#if>
  <#if methodType == 'lt'>
    <#return 'lt'/>
  </#if>
  <#if methodType == 'le'>
    <#return 'le'/>
  </#if>
  <#return 'eq'/>
</#function>
package ${basePackage}.service.${moduleName}.impl;

import ${basePackage}.core.model.PageData;
import ${basePackage}.core.model.PageWrap;
import ${basePackage}.core.utils.Utils;
import ${basePackage}.dao.${moduleName}.${table.pascalCaseName}Mapper;
import ${basePackage}.dao.${moduleName}.model.${table.pascalCaseName};
import ${basePackage}.service.${moduleName}.${table.pascalCaseName}Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * ${table.comment}业务方法实现
 */
@Service
public class ${table.pascalCaseName}ServiceImpl implements ${table.pascalCaseName}Service {

    @Autowired
    private ${table.pascalCaseName}Mapper ${table.camelCaseName}Mapper;

    @Override
    public ${pkType} create(${table.pascalCaseName} dto) {
        ${table.camelCaseName}Mapper.insert(dto);
        return dto.${getPk}();
    }

    @Override
    public void deleteById(${pkType} id) {
        ${table.camelCaseName}Mapper.deleteById(id);
    }

    @Override
    public void deleteByIdInBatch(List<${pkType}> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        ${table.camelCaseName}Mapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(${table.pascalCaseName} dto) {
        ${table.camelCaseName}Mapper.updateById(dto);
    }

    @Override
    public void updateByIdInBatch(List<${table.pascalCaseName}> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return;
        }
        for (${table.pascalCaseName} dto: dtos) {
            this.updateById(dto);
        }
    }

    @Override
    public ${table.pascalCaseName} findById(${pkType} id) {
        return ${table.camelCaseName}Mapper.selectById(id);
    }

    @Override
    public ${table.pascalCaseName} findOne(${table.pascalCaseName} dto) {
        QueryWrapper<${table.pascalCaseName}> wrapper = new QueryWrapper<>(dto);
        return ${table.camelCaseName}Mapper.selectOne(wrapper);
    }

    @Override
    public List<${table.pascalCaseName}> findList(${table.pascalCaseName} dto) {
        QueryWrapper<${table.pascalCaseName}> wrapper = new QueryWrapper<>(dto);
        return ${table.camelCaseName}Mapper.selectList(wrapper);
    }

    @Override
    public PageData<${table.pascalCaseName}> findPage(PageWrap<${table.pascalCaseName}> pageWrap) {
        IPage<${table.pascalCaseName}> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<${table.pascalCaseName}> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        <#if table.queryFields?size gt 0>
        queryWrapper.lambda()
        </#if>
        <#list table.queryFields as field>
        <#-- 日期:等于处理 -->
        <#if getType(field) == 'Date' && field.queryMethod == 'eq'>
                .ge(pageWrap.getModel().get${field.pascalCaseName}() != null, ${table.pascalCaseName}::get${field.pascalCaseName}, Utils.Date.getStart(pageWrap.getModel().get${field.pascalCaseName}()))
                .le(pageWrap.getModel().get${field.pascalCaseName}() != null, ${table.pascalCaseName}::get${field.pascalCaseName}, Utils.Date.getEnd(pageWrap.getModel().get${field.pascalCaseName}()))
        <#else>
        <#-- 其它处理 -->
                .${getQueryMethod(field.queryMethod)}(pageWrap.getModel().get${field.pascalCaseName}() != null, ${table.pascalCaseName}::get${field.pascalCaseName}, pageWrap.getModel().get${field.pascalCaseName}())
        </#if>
        </#list>
        <#if table.queryFields?size gt 0>
        ;
        </#if>
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(${table.camelCaseName}Mapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(${table.pascalCaseName} dto) {
        QueryWrapper<${table.pascalCaseName}> wrapper = new QueryWrapper<>(dto);
        return ${table.camelCaseName}Mapper.selectCount(wrapper);
    }
}
